﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.ComponentModel;

namespace BaseLibrary
{
    public class InstallOutOfBrowserBehavior  : TargetedTriggerAction<FrameworkElement>
    {
        UIElement _element;

        protected override void OnAttached()
        {
            _element = this.Target as UIElement;

            if (!Application.Current.IsRunningOutOfBrowser)
            {
                 Application.Current.InstallStateChanged += (s, e) =>
                    {
                        CheckInstallState();
                    };

                CheckInstallState();

                if (_element is Button)
                {
                    (_element as Button).Click += (s, e) =>
                        {
                            InstallOOB();
                        };
                }
                else
                {
                    _element.MouseLeftButtonDown += (s, e) =>
                    {
                        InstallOOB();
                    };
                }
            }
            else
            {
                _element.Visibility = Visibility.Collapsed;
            }
            base.OnAttached();
        }

        private void InstallOOB()
        {
            if (Application.Current.InstallState != InstallState.Installed &&
                Application.Current.InstallState != InstallState.Installing)
            {
                try
                {
                    Application.Current.Install();
                }
                catch 
                {
                    MessageBox.Show(ErrorMessage);
                }
            }
        }

        private void CheckInstallState()
        {
            if (Application.Current.InstallState == InstallState.Installed ||
                Application.Current.InstallState == InstallState.Installing)
            {
                _element.Visibility = Visibility.Collapsed;
            }
            else
            {
                _element.Visibility = Visibility.Visible;
            }
        }

        public string ErrorMessage
        {
            get { return (string)GetValue(ErrorMessageProperty); }
            set { SetValue(ErrorMessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ErrorMessage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ErrorMessageProperty =
            DependencyProperty.Register(
                "ErrorMessage", 
                typeof(string), 
                typeof(InstallOutOfBrowserBehavior), 
                new PropertyMetadata("Out of Browser Mode is not activated for this application."));

        protected override void Invoke(object o)
        {
            // Do nothing
        }
    }
}
